home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Recursive Shell 1.0.1 / Examples / Count Items ƒ / Utility.c < prev    next >
Text File  |  1996-06-17  |  6KB  |  285 lines

  1. /*****************************************************************************/
  2. //
  3. //    Utility.c
  4. //
  5. //    Various utility functions for use in the recursive shell program.
  6. //
  7. //    Version 1.0.1
  8. //
  9. //    Created:    13 May 1996
  10. //    Modified:    17 June 1996
  11. //
  12. /*****************************************************************************/
  13.  
  14. #include    "Utility.h"
  15.  
  16.  
  17. //    Prototypes
  18.  
  19.  
  20. //    BusyCursor
  21. //
  22. //    Changes the cursor into the watch.
  23. void    BusyCursor( void )
  24. {
  25.     CursHandle    myCursorHandle;
  26.     
  27.     myCursorHandle = GetCursor( watchCursor );
  28.     if ( myCursorHandle != nil )
  29.         SetCursor( *myCursorHandle );
  30. }
  31.  
  32.  
  33. //
  34. //    SysSevenOrBetter
  35. //
  36. //    Returns true if the current system version is 7.0 or higher.
  37. //    Returns false otherwise.
  38. //
  39. Boolean    SysSevenOrBetter ( void )
  40. {
  41.     SysEnvRec    myEnviron;
  42.     OSErr        myErr;
  43.     
  44.     //    Oddly enough, Apple discourages the use of Gestalt to determine
  45.     //    current operating system version.  So, we use SysEnvirons instead:
  46.     
  47.     myErr = SysEnvirons( curSysEnvVers, &myEnviron );
  48.     if ( myErr != noErr )
  49.         return false;
  50.     
  51.     if ( myEnviron.systemVersion >= 0x0700 )
  52.         return true;
  53.     else
  54.         return false;
  55. }
  56.  
  57.  
  58. //
  59. //    IsAFolder
  60. //
  61. //    Returns true if theSpec is a folder, false otherwise.
  62. //    Code taken from Andy Duncan's Scruncher code.
  63. //
  64. Boolean IsAFolder(FSSpec* theSpec)
  65. {
  66.     CInfoPBRec     pb;
  67.     OSErr        err;
  68.     
  69.     pb.hFileInfo.ioCompletion = (IOCompletionUPP) NULL;
  70.     pb.hFileInfo.ioNamePtr = theSpec->name;
  71.     pb.hFileInfo.ioVRefNum = theSpec->vRefNum;
  72.     pb.hFileInfo.ioFDirIndex = 0;        // Find out about this file.
  73.     pb.hFileInfo.ioDirID = theSpec->parID;
  74.     
  75.     err = PBGetCatInfo(&pb, false);
  76.     if (err != noErr) 
  77.         return false;
  78.     else
  79.         return ((pb.hFileInfo.ioFlAttrib & (1 << 4)) != 0);
  80. }
  81.  
  82.  
  83. //
  84. //    AppendColon
  85. //
  86. //    Adds a colon to the end of the Pascal string dirName.
  87. //
  88. void    AppendColon( Str255 dirName )
  89. {
  90.     short    length;
  91.     
  92.     length = dirName[0];
  93.     length++;
  94.     dirName[0] = length;
  95.     dirName[length] = ':';
  96. }
  97.  
  98.  
  99. //
  100. //    CopyPString
  101. //
  102. //    Copies all of Pascal string copyFrom into copyTo.
  103. //
  104. void    CopyPString( Str255 copyFrom, Str255 copyTo )
  105. {
  106.     short    i;
  107.     
  108.     for ( i = 0; i <= copyFrom[0]; i++ )
  109.         copyTo[i] = copyFrom[i];
  110. }
  111.  
  112.  
  113. //
  114. //    ConcatPStrings
  115. //
  116. //    Contaenates Pascal strings aStr and bStr, placing the result into destStr.
  117. //
  118. void    ConcatPStrings( Str255 aStr, Str255 bStr, Str255 destStr )
  119. {
  120.     short    i, j, length, aLength, bLength;
  121.     short    aCount, bCount;
  122.     Str255    firstString;
  123.     Str255    secondString;
  124.     
  125.     CopyPString( aStr, firstString );
  126.     CopyPString( bStr, secondString );
  127.     
  128.     aLength = firstString[0];
  129.     bLength = secondString[0];
  130.     length = aLength + bLength;
  131.     
  132.     aCount = 1;
  133.     bCount = 1;
  134.     
  135.     if ( length < 256 )
  136.     {
  137.         destStr[0] = length;
  138.         
  139.         for ( i = 1; i <= aLength; i++ )
  140.             destStr[i] = firstString[i];
  141.         
  142.         for ( j = 1; i <= length; i++, j++ )
  143.             destStr[i] = secondString[j];
  144.         
  145.     }
  146.     else
  147.     {
  148.         destStr[0] = aLength;
  149.         
  150.         for ( i = 1; i <= aLength; i++ )
  151.             destStr[i] = firstString[i];
  152.     }
  153. }
  154.  
  155.  
  156. //
  157. //    GetFullPath
  158. //
  159. //    Puts the full path name of theSpec into pathName.  Based on sample code
  160. //    taken from New Inside Macintosh: Files.
  161. //
  162. void    GetFullPath( FSSpec* theSpec, Str255 pathName )
  163. {
  164.     CInfoPBRec    pb;
  165.     Str255        dirName, fullPath, fileName;
  166.     OSErr        err;
  167.     Boolean        done = false;
  168.     
  169.     fullPath[0] = '\0';
  170.     dirName[0] = '\0';
  171.     
  172.     CopyPString( theSpec->name, fileName );
  173.     if ( ( IsAFolder( theSpec ) ) && ( theSpec->name[0] != '\0' ) )
  174.         AppendColon( fileName );
  175.     
  176.     pb.hFileInfo.ioNamePtr = dirName;
  177.     pb.hFileInfo.ioVRefNum = theSpec->vRefNum;
  178.     pb.hFileInfo.ioFDirIndex = -1;                //    Get info about a directory
  179.     pb.dirInfo.ioDrParID = theSpec->parID;
  180.     
  181.     while ( !done )
  182.     {
  183.         //    Change ID of directory we're getting info for to parent of one
  184.         //    we just got info for:
  185.         pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  186.         //    Get info for new (parent) directory:
  187.         err = PBGetCatInfo( &pb, false );
  188.         
  189.         if ( err != noErr )
  190.             done = true;
  191.         else
  192.         {
  193.             //    Add a colon to the end of the name of the new (parent) directory:
  194.             AppendColon( dirName );
  195.             //    Put the name of the directory before the full pathname:
  196.             ConcatPStrings( dirName, fullPath, fullPath );
  197.             
  198.             if ( pb.dirInfo.ioDrDirID == fsRtDirID )
  199.                 done = true;
  200.         }
  201.     }
  202.     ConcatPStrings( fullPath, fileName, pathName );
  203. }
  204.  
  205.  
  206. //
  207. //    ResolveOSErr
  208. //
  209. //    Takes those evil OSErr numbers (or at least the ones we're likely to
  210. //    come across) and puts them in slightly more human-readable form.
  211. //
  212. void    ResolveOSErr( OSErr    inErr, Str255 returnValue )
  213. {
  214.     switch ( inErr )
  215.     {
  216.         case noErr:
  217.             CopyPString( "\pERROR: No error", returnValue );
  218.             break;
  219.         case fBsyErr:
  220.             CopyPString( "\pERROR: File busy, in use, or folder not empty", returnValue );
  221.             break;
  222.         case afpObjectTypeErr:
  223.             CopyPString( "\pERROR: A directory exists with that name", returnValue );
  224.             break;
  225.         case dskFulErr:
  226.             CopyPString( "\pERROR: Disk is full", returnValue );
  227.             break;
  228.         case dirFulErr:
  229.             CopyPString( "\pERROR: File directory is full", returnValue );
  230.             break;
  231.         case tmfoErr:
  232.             CopyPString( "\pERROR: Too many files open", returnValue );
  233.             break;
  234.         case fnOpnErr:
  235.             CopyPString( "\pERROR: File not open", returnValue );
  236.             break;
  237.         case rfNumErr:
  238.             CopyPString( "\pERROR: Bad File Ref number", returnValue );
  239.             break;
  240.         case wPrErr:
  241.         case vLckdErr:
  242.             CopyPString( "\pERROR: Disk is locked", returnValue );
  243.             break;
  244.         case permErr:
  245.         case fLckdErr:
  246.             CopyPString( "\pERROR: File is locked", returnValue );
  247.             break;
  248.         case wrPermErr:
  249.             CopyPString( "\pERROR: Write access denied", returnValue );
  250.             break;
  251.         case opWrErr:
  252.             CopyPString( "\pERROR: File is already open", returnValue );
  253.             break;
  254.         case nsvErr:
  255.             CopyPString( "\pERROR: No such volume", returnValue );
  256.             break;
  257.         case ioErr:
  258.             CopyPString( "\pERROR: I/O error", returnValue );
  259.             break;
  260.         case bdNamErr:
  261.             CopyPString( "\pERROR: Bad filename", returnValue );
  262.             break;
  263.         case fnfErr:
  264.         case dirNFErr:
  265.             CopyPString( "\pERROR: Directory not found or incomplete pathname", returnValue );
  266.             break;
  267.         case dupFNErr:
  268.             CopyPString( "\pERROR: Duplicate filename and version", returnValue );
  269.             break;
  270.         case afpAccessDenied:
  271.             CopyPString( "\pERROR: User does not have the correct access", returnValue );
  272.             break;
  273.         case paramErr:
  274.             CopyPString( "\pERROR: Negative count (-50)", returnValue );
  275.             break;
  276.         case posErr:
  277.             CopyPString( "\pERROR: Attempt to position mark before start of file (-40)", returnValue );
  278.             break;
  279.         default:
  280.             CopyPString( "\pERROR: Unknown Error", returnValue );
  281.             break;
  282.     }
  283. }
  284.  
  285.